package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

@Api(tags = "商店营业状态接口")
@RestController("AdminShopController")
@RequestMapping("admin/shop")
@Slf4j
public class ShopController {
    private static final String KEY = "SHOP_STATUS";
    @Autowired
    private RedisTemplate  redisTemplate;

    /**
     * 设置店铺营业状态
     * @param status
     * @return
     */
    @ApiOperation("设置店铺营业状态")
    @PutMapping({"/{status}"})
    public Result changeStatus(@PathVariable Integer status){
        log.info("设置店铺营业状态：{}",status);
        //1.调用redisTemplate将店铺状态存入到redis中
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(KEY,status);
        //2.返回成功
        return Result.success();
    }

    /**
     * 查询店铺营业状态
     * @return
     */
    @ApiOperation("商家获取店铺营业状态")
    @GetMapping("/status")
    public Result getStatus(){
        log.info("获取店铺营业状态");
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Integer status = (Integer) valueOperations.get(KEY);
        return Result.success(status);
    }
}
